package jprint.jfx.http;

import java.io.ByteArrayInputStream;
import javax.annotation.Nonnull;
import jprint.base.AppContext;
import jprint.base.http.AbstractRequestHandler;
import jprint.base.http.Http;
import jprint.base.label.Label;
import jprint.base.label.LabelPage;
import jprint.base.label.LabelService;

/**
 * @author zw-zheng
 * Created on 2021-02-04
 */
class DesignHandler extends AbstractRequestHandler {

    private AppContext appContext;
    private final LabelService labelService;

    DesignHandler(@Nonnull AppContext appContext) {
        this.appContext = appContext;
        labelService = LabelService.getInstance(appContext);
    }

    @Override public boolean handle(Http.HttpRequest request, Http.HttpResponse response) {
        if (request.uri().equals("/api/design-preview")) {
            Label label = labelService.labelFormat("default-format");
            preview(label, response);
            return true;
        }
        return false;
    }

    private void preview(Label label, Http.HttpResponse response) {
        LabelPage page = new LabelPage(label, appContext);
        response.setContentType(contentTypeOf("dummy.png"));
        response.write(new ByteArrayInputStream(page.getPngBytes()));
    }
}
